$(document).ready(function()
{
  $(".up").click(function()
  {
    var row = $(this).parents("tr:first");
    var id = Number($(this).attr('id'));
    if ((row.prev().attr('id')!='tblh') && $(this).is(".up"))
    {
      $("#loading-mask").css('display', '');      
      $("#loading-msg").css('display', '');
      if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
      else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      xmlhttp.onreadystatechange=function()
      {		
        if(xmlhttp.readyState==4)
        {
          $("#loading-mask").css('display', 'none');
          $("#loading-msg").css('display', 'none');        
          if (xmlhttp.responseText=="1")
          {                                     
            row.insertBefore(row.prev());                        
          }
          else { alert(xmlhttp.responseText); }          
        }
      }
      var url=document.getElementById("ajax_url").value;
      url = url + "?op=co&id=" + id;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);        
    }     
  });
});
function updateSchedule(id)
{
  $("#loading-mask").css('display', '');
  $("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
  else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange=function()
	{		
		if(xmlhttp.readyState==4)
		{
      $("#loading-mask").css('display', 'none');
      $("#loading-msg").css('display', 'none');
			if (xmlhttp.responseText!="1")      
      {
        alert(xmlhttp.responseText);
      }
		}
	}
	var url=document.getElementById("ajax_url").value + "?op=us&id=" + id;	
  var s = new Array(); var i=0;
  for (var a=0;a<4;a++)
  {
    for (var b=0;b<7;b++)
    {
      if (document.getElementById('s_'+id+'_'+a+'_'+b).checked)
      s[i++] = (a+b*4);
    }
  }
  url = url + "&s=" + s.join(",");
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function updateSalary(id)
{
	$("#loading-mask").css('display', '');
	$("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	xmlhttp.onreadystatechange=function()
	{		
		if(xmlhttp.readyState==4)
		{
		  $("#loading-mask").css('display', 'none');
		  $("#loading-msg").css('display', 'none');
		  if (xmlhttp.responseText!="1")      
		  {
			alert(xmlhttp.responseText);
		  }
		}
	}
	if(document.getElementById("choose_salary_shift_"+id).checked)
	{
		var sat = 0;
		var sa = document.getElementById("salary_shift_"+id).value;

	}
	else
	{
		var sat = 1;
		var sa = document.getElementById("salary_month_"+id).value;
	}
	var url=document.getElementById("ajax_url").value + "?op=usa&id=" + id;	
		url = url + "&sat=" + sat ;
		url = url + "&sa=" + sa;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function calc_hours_salary(id)
{
	var scaled_hours = 0;
	var total_hours = 0;
	var salary = 0;
	var ratio = {
			"first_shift":2
			,"last_shift":1.5
			,"weekend":2
			,"last_shift_weekend":2.5
	};
	
	var hours_per_shift = 4;
	var salary_per_shift = document.getElementById("salary_shift_"+id).value;
	
	for (var x=0;x<4;x++)
	{
		for (var y=0;y<7;y++)
		{
			if(document.getElementById("s_"+id+"_"+x+"_"+y).checked)
			{
				//total_hours
				if(x == 0)
					total_hours += 8;
				else
					total_hours += 4;

				//scaled hours
				if(x == 0 && (y == 5 || y == 6))
					scaled_hours = scaled_hours + hours_per_shift * ratio["first_shift"] * ratio["weekend"];
				else if(x == 3 && (y == 5 || y == 6))
					scaled_hours = scaled_hours + hours_per_shift * ratio["last_shift_weekend"];
				else if(x == 0)
					scaled_hours = scaled_hours + hours_per_shift * ratio["first_shift"];
				else if(x == 3)
					scaled_hours = scaled_hours + hours_per_shift * ratio["last_shift"];
				else if(y == 5 || y == 6)
					scaled_hours = scaled_hours + hours_per_shift * ratio["weekend"];
				else
					scaled_hours = scaled_hours + hours_per_shift;
			}					
		}
	}
	if(document.getElementById("choose_salary_shift_"+id).checked)
	{		
			document.getElementById("salary_week_"+id).value = salary_per_shift * scaled_hours/4;
			document.getElementById("salary_month_"+id).value = salary_per_shift * scaled_hours;
	}
	else if(document.getElementById("choose_salary_month_"+id).checked)
	{		
			document.getElementById("salary_week_"+id).value = document.getElementById("choose_salary_month_"+id).value / 4;
			document.getElementById("salary_shift_"+id).value = document.getElementById("salary_week_"+id).value * 4 / scaled_hours;
	}
	document.getElementById("total_hours_"+id).value = total_hours;
	document.getElementById("scaled_hours_"+id).value = scaled_hours;
}
function chooseKindOfSalary(id)
{
	if(document.getElementById("choose_salary_shift_"+id).checked)
	{
		document.getElementById("salary_shift_"+id).readOnly = false;
		document.getElementById("salary_month_"+id).readOnly = true;
	}
	else
	{
		document.getElementById("salary_shift_"+id).disabled = true;
		document.getElementById("salary_month_"+id).readOnly = false;
	}
}
function delRequest(adminid,date,shiftid)
{
	$("#loading-mask").css('display', '');
	$("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	
	var url = document.getElementById("ajax_url").value + "?op=delRequest";
		url = url + "&adminid=" + adminid;
		url = url + "&date=" + date;
		url = url + "&shiftid=" + shiftid;

	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			$("#loading-mask").css('display', 'none');
			$("#loading-msg").css('display', 'none');
			if (xmlhttp.responseText!="1")      
			{
				alert(xmlhttp.responseText);
			}
		}
	}
	xmlhttp.send(null);
}
function rejectRequest(adminid,date,shiftid)
{
	$("#loading-mask").css('display', '');
	$("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	
	var url = document.getElementById("ajax_url").value + "?op=rejectRequest";
		url = url + "&adminid=" + adminid;
		url = url + "&date=" + date;
		url = url + "&shiftid=" + shiftid;

	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			$("#loading-mask").css('display', 'none');
			$("#loading-msg").css('display', 'none');
			if (xmlhttp.responseText!="1")      
			{
				alert(xmlhttp.responseText);
			}
		}
	}
	xmlhttp.send(null);
}

function approveRequest(adminid,date,shiftid)
{
	$("#loading-mask").css('display', '');
	$("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	
	var url = document.getElementById("ajax_url").value + "?op=approveRequest";
		url = url + "&adminid=" + adminid;
		url = url + "&date=" + date;
		url = url + "&shiftid=" + shiftid;

	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			$("#loading-mask").css('display', 'none');
			$("#loading-msg").css('display', 'none');
			if (xmlhttp.responseText!="1")      
			{
				alert(xmlhttp.responseText);
			}
		}
	}
	xmlhttp.send(null);
}

function filter(column,value)
{
	$("#loading-mask").css('display', '');
	$("#loading-msg").css('display', '');
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	
	var url = document.getElementById("ajax_url").value + "?op=filter";
		url = url + "&column=" + column;
		url = url + "&value=" + value;

	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			$("#loading-mask").css('display', 'none');
			$("#loading-msg").css('display', 'none');
			document.getElementById("dayoff").innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}